package com.ht.utils;

import com.ht.utils.config.WordNumberConfig;
import com.ht.utils.htmlBean.*;
import lombok.extern.slf4j.Slf4j;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * word文档与html相互转换 这一版只做了docx文件转html代码，后续如果有需要则继续补充
 */
@Slf4j
public class DocHtmlConvertUtil {
    private static HashMap<String, String> wordNumberFormat;
    private static HashMap<String, Integer> numberFormat_num;
    private static int imageNum = 0;

    /**
     * MultipartFile 转 File
     *
     * @param multipartFile 从前端中取得的文件
     */
    private static File multipartFileToFile(MultipartFile multipartFile) {
        File resultFile = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
            outputStream = new FileOutputStream(resultFile);
            int bytesRead;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            CloseStreamUtil.closeStream(inputStream, outputStream);

        }
        return resultFile;
    }

    /**
     * 将docx转为html
     *
     * @param multipartFile 从前端中取得的文件
     * @param contextPath   html展示到前台后的请求图片的接口
     * @param uploadPath    docx文档中的图片保存路径
     * @return html代码
     */
    public static String docxToHtml(MultipartFile multipartFile, String contextPath, String uploadPath) {
        try {
            numberFormat_num = new HashMap<>();
            imageNum = 0;
            File docxFile = multipartFileToFile(multipartFile);
            String docxFileName = docxFile.getName();
            String docxFileNameSuffix = docxFileName.substring(docxFileName.lastIndexOf(".") + 1);
            if (!"docx".equals(docxFileNameSuffix)) {
                throw new RuntimeException("请上传docx文件！");
            }
            // 判断上传路径是否存在，如果没有则创建新文件夹
            String docxFilePath = uploadPath + docxFileName.substring(0, docxFileName.lastIndexOf("."));
            if (!new File(docxFilePath).exists()) {
                boolean r = new File(docxFilePath).mkdirs();
                if (!r) {
                    throw new RuntimeException("创建文件夹失败！");
                }
            }
            WordprocessingMLPackage wordMLPackage = null;
            wordMLPackage = WordprocessingMLPackage.load(docxFile);

            if (wordMLPackage == null) {
                throw new RuntimeException("wordMLPackage load error!");
            }
            MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
            NumberingDefinitionsPart numberingDefinitionsPart = documentPart.getNumberingDefinitionsPart();
            if (numberingDefinitionsPart == null) {
                throw new RuntimeException("请检查word文件是否有numbering.xml文件!");
            }
            wordNumberFormat = WordNumberConfig.getCurrentFormatNumber(numberingDefinitionsPart.getXML());
            List<String> imagePaths = new LinkedList<>();
            for (Map.Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
                if (entry.getValue() instanceof BinaryPartAbstractImage binImg) {
                    PartName pt = binImg.getPartName();
                    String fileName;
                    if (pt.getName().contains("word/media/")) {
                        fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
                    } else {
                        fileName = "errorFileName";
                    }
                    FileOutputStream fos = null;
                    String imagePath;
                    // 请求图片的接口 + 图片地址
                    imagePath = contextPath
                            + docxFileName.substring(0, docxFileName.lastIndexOf("."))
                            + "/"
                            + fileName.substring(0, fileName.lastIndexOf("."))
                            + ".png";

                    imagePaths.add(imagePath);
                    try {
                        fos = new FileOutputStream(uploadPath
                                + docxFileName.substring(0, docxFileName.lastIndexOf("."))
                                + "/"
                                + fileName.substring(0, fileName.lastIndexOf("."))
                                + ".png");
                        ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    } finally {
                        assert fos != null;
                        fos.close();
                    }
                }
            }
            imagePaths.sort((o1, o2) -> {
                int flag = o1.length() - o2.length();
                return o1.compareTo(o2) + 10 * flag;
            });
            // 因为MultipartFile转File文件时，会在项目根目录生成临时文件
            // 所以在生成html代码后，要清理临时文件
            boolean delete = docxFile.delete();
            if (!delete) {
                log.error("删除临时文件失败！");
            }
            return analysisDocxXml(documentPart.getXML(), imagePaths);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    private static String analysisDocxXml(String xml, List<String> imagePaths) {
        SAXReader saxReader = new SAXReader();
        Table table;
        Html html = new Html();
        try {
            Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
            Element rootELement = document.getRootElement();
            Iterator<?> it = rootELement.elementIterator();
            while (it.hasNext()) {
                Element firstChild = (Element) it.next();
                //获取子节点的下一级节点
                Iterator<?> iterator = firstChild.elementIterator();
                Element sectPr = firstChild.element("sectPr");
                Iterator<?> it_sectPr = sectPr.elementIterator();
                while (it_sectPr.hasNext()) {
                    Element one = (Element) it_sectPr.next();
                    switch (one.getName()) {
                        case "pgMar":
                            html.setBottom(1440);
                            html.setLeft(1800);
                            html.setRight(1800);
                            html.setTop(1440);
                            break;
                        case "pgSz":
                            html.setWidth(Float.parseFloat(one.attributeValue("w")));
                            html.setHeight(Float.parseFloat(one.attributeValue("h")));
                            break;
                    }
                }
                while (iterator.hasNext()) {
                    Element element = (Element) iterator.next();
                    switch (element.getName()) {
                        case "p":
                            html.addObj(analyPPR(element, imagePaths));
                            break;
                        case "tbl":
                            Iterator<?> it_tbl = element.elementIterator();
                            table = new Table();
                            HashMap<Integer, Integer> tc_row = new HashMap<>();
                            int currentTr = 0;
                            int currentTc = 0;
                            while (it_tbl.hasNext()) {
                                Element one_tbl = (Element) it_tbl.next();
                                switch (one_tbl.getName()) {
                                    case "tblPr":
                                        Iterator<?> it_tbl_tblPr = one_tbl.elementIterator();
                                        while (it_tbl_tblPr.hasNext()) {
                                            Element one_tbl_tblPr = (Element) it_tbl_tblPr.next();
                                            switch (one_tbl_tblPr.getName()) {
                                                case "tblW":
                                                    break;
                                                case "tblBorders":
                                                    Iterator<?> it_tbl_tblPr_tblBorders = one_tbl_tblPr.elementIterator();
                                                    while (it_tbl_tblPr_tblBorders.hasNext()) {
                                                        Element one_tbl_tblPr_tblBorders = (Element) it_tbl_tblPr_tblBorders.next();
                                                        switch (one_tbl_tblPr_tblBorders.getName()) {
                                                            case "top":
                                                                table.setTop(new Line(one_tbl_tblPr_tblBorders.attributeValue("val"),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tblPr_tblBorders.attributeValue("color")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "left":
                                                                table.setLeft(new Line(one_tbl_tblPr_tblBorders.attributeValue("val"),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tblPr_tblBorders.attributeValue("color")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "bottom":
                                                                table.setBottom(new Line(one_tbl_tblPr_tblBorders.attributeValue("val"),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tblPr_tblBorders.attributeValue("color")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "right":
                                                                table.setRight(new Line(one_tbl_tblPr_tblBorders.attributeValue("val"),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tblPr_tblBorders.attributeValue("color")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tblPr_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tblPr_tblBorders.attributeValue("space"))));
                                                                break;
                                                        }
                                                    }
                                                    break;
                                                case "tblpPr":
                                                    if (Float.parseFloat(one_tbl_tblPr.attributeValue("tblpX")) != html.getLeft()
                                                            && Float.parseFloat(one_tbl_tblPr.attributeValue("tblpX")) != 1800) {
                                                        table.setMargin_left((float) (Float.parseFloat(one_tbl_tblPr.attributeValue("tblpX")) / 1440 * 72 / 0.75));
                                                    }
                                                    if (Float.parseFloat(one_tbl_tblPr.attributeValue("tblpY")) != html.getTop()
                                                            && Float.parseFloat(one_tbl_tblPr.attributeValue("tblpY")) != 1440) {
                                                        table.setMargin_top((float) (Float.parseFloat(one_tbl_tblPr.attributeValue("tblpY")) / 1440 * 72 / 0.75));
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                    case "tr":
                                        Iterator<?> it_tbl_tr = one_tbl.elementIterator();
                                        TR tr = new TR();
                                        while (it_tbl_tr.hasNext()) {
                                            Element one_tbl_tr = (Element) it_tbl_tr.next();
                                            switch (one_tbl_tr.getName()) {
                                                case "tblPrEx":
                                                    Element one_tbl_tr_tblPrEx = one_tbl_tr.element("tblBorders");
                                                    Iterator<?> it_tbl_tr_tblPrEx_tblBorders = one_tbl_tr_tblPrEx.elementIterator();
                                                    while (it_tbl_tr_tblPrEx_tblBorders.hasNext()) {
                                                        Element one_tbl_tr_tblPrEx_tblBorders = (Element) it_tbl_tr_tblPrEx_tblBorders.next();
                                                        switch (one_tbl_tr_tblPrEx_tblBorders.getName()) {
                                                            case "left":
                                                                tr.setLeft(new Line(one_tbl_tr_tblPrEx_tblBorders.attributeValue("val"),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tr_tblPrEx_tblBorders.attributeValue("color")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "top":
                                                                tr.setTop(new Line(one_tbl_tr_tblPrEx_tblBorders.attributeValue("val"),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tr_tblPrEx_tblBorders.attributeValue("color")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "bottom":
                                                                tr.setBottom(new Line(one_tbl_tr_tblPrEx_tblBorders.attributeValue("val"),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tr_tblPrEx_tblBorders.attributeValue("color")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("space"))));
                                                                break;
                                                            case "right":
                                                                tr.setRight(new Line(one_tbl_tr_tblPrEx_tblBorders.attributeValue("val"),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("color") == null ? null :
                                                                                ("#" + one_tbl_tr_tblPrEx_tblBorders.attributeValue("color")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("sz")),
                                                                        one_tbl_tr_tblPrEx_tblBorders.attributeValue("space") == null ? -1 :
                                                                                Float.parseFloat(one_tbl_tr_tblPrEx_tblBorders.attributeValue("space"))));
                                                                break;
                                                        }
                                                    }
                                                    break;
                                                case "tc":
                                                    TC tc = new TC();
                                                    Element one_tcPr = one_tbl_tr.element("tcPr");
                                                    Element vMerge = one_tcPr.element("vMerge");
                                                    if (vMerge != null) {
                                                        if (tc_row.get(currentTc) == null) {
                                                            tc_row.put(currentTc, currentTr);
                                                            tc.setRowSpan(1);
                                                        } else {
                                                            if ("restart".equals(vMerge.attributeValue("val"))) {
                                                                tc_row.put(currentTc, currentTr);
                                                                tc.setRowSpan(1);
                                                            } else {
                                                                table.getTrs().get(tc_row.get(currentTc)).getTcs().get(currentTc).addRowSpan();
                                                                tc.setHidden(true);
                                                            }
                                                        }
                                                    }
                                                    Element gridSpan = one_tcPr.element("gridSpan");
                                                    if (gridSpan != null) {
                                                        tc.setGridSpan(Integer.parseInt(gridSpan.attributeValue("val")));
                                                    }
                                                    Element one_tcPr_tcW = one_tcPr.element("tcW");
                                                    if (one_tcPr_tcW != null)
                                                        tc.setWidth(Float.parseFloat(one_tcPr_tcW.attributeValue("w")) / table.getWidth());

                                                    Element tc_border = one_tcPr.element("tcBorders");
                                                    if (tc_border != null) {
                                                        Iterator<?> it_tc_border = tc_border.elementIterator();
                                                        while (it_tc_border.hasNext()) {
                                                            Element one_tc_border_line = (Element) it_tc_border.next();
                                                            switch (one_tc_border_line.getName()) {
                                                                case "top":
                                                                    tc.setTop(new Line(one_tc_border_line.attributeValue("val"),
                                                                            one_tc_border_line.attributeValue("color") == null ? null :
                                                                                    ("#" + one_tc_border_line.attributeValue("color")),
                                                                            one_tc_border_line.attributeValue("sz") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("sz")),
                                                                            one_tc_border_line.attributeValue("space") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("space"))));
                                                                    break;
                                                                case "bottom":
                                                                    tc.setBottom(new Line(one_tc_border_line.attributeValue("val"),
                                                                            one_tc_border_line.attributeValue("color") == null ? null :
                                                                                    ("#" + one_tc_border_line.attributeValue("color")),
                                                                            one_tc_border_line.attributeValue("sz") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("sz")),
                                                                            one_tc_border_line.attributeValue("space") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("space"))));
                                                                    break;
                                                                case "left":
                                                                    tc.setLeft(new Line(one_tc_border_line.attributeValue("val"),
                                                                            one_tc_border_line.attributeValue("color") == null ? null :
                                                                                    ("#" + one_tc_border_line.attributeValue("color")),
                                                                            one_tc_border_line.attributeValue("sz") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("sz")),
                                                                            one_tc_border_line.attributeValue("space") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("space"))));
                                                                    break;
                                                                case "right":
                                                                    tc.setRight(new Line(one_tc_border_line.attributeValue("val"),
                                                                            one_tc_border_line.attributeValue("color") == null ? null :
                                                                                    ("#" + one_tc_border_line.attributeValue("color")),
                                                                            one_tc_border_line.attributeValue("sz") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("sz")),
                                                                            one_tc_border_line.attributeValue("space") == null ? -1 :
                                                                                    Float.parseFloat(one_tc_border_line.attributeValue("space"))));
                                                                    break;

                                                            }
                                                        }
                                                    }
                                                    Element shd = one_tcPr.element("shd");
                                                    if (shd != null) {
                                                        tc.setColor(shd.attributeValue("color"));
                                                        tc.setBackground_color(shd.attributeValue("fill"));
                                                    }
                                                    List<Element> p = one_tbl_tr.elements("p");
                                                    if (p != null && !p.isEmpty()) {
                                                        for (Element element1 : p) {
                                                            PPR ppr = analyPPR(element1, imagePaths);
                                                            ppr.setTitleLev("table-p");
                                                            tc.addPpr(ppr);
                                                        }
                                                    }
                                                    currentTc++;
                                                    tr.addTc(tc);
                                                    break;
                                            }
                                        }
                                        currentTr++;
                                        currentTc = 0;
                                        table.addTr(tr);
                                        break;
                                    case "tblGrid":
                                        Iterator<?> it_tbl_tblGrid = one_tbl.elementIterator();
                                        while (it_tbl_tblGrid.hasNext()) {
                                            Element one_tbl_tblGrid = (Element) it_tbl_tblGrid.next();
                                            if (one_tbl_tblGrid.getName().equals("gridCol")) {
                                                table.addWidths(Float.parseFloat(one_tbl_tblGrid.attributeValue("w")));
                                            }
                                        }
                                        break;
                                }
                            }
                            html.addObj(table);
                            break;
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return html.toString();
    }

    private static PPR analyPPR(Element element, List<String> imagePaths) {
        int imagePathsSize = imagePaths.size();
        PPR ppr = new PPR();
        Iterator<?> it_3 = element.elementIterator();
        while (it_3.hasNext()) {
            Element one = (Element) it_3.next();
            switch (one.getName()) {
                case "pPr":
                    Iterator<?> it_4_ppr = one.elementIterator();
                    while (it_4_ppr.hasNext()) {
                        Element one_it_4_ppr = (Element) it_4_ppr.next();
                        switch (one_it_4_ppr.getName()) {
                            case "pStyle":
                                ppr.setTitleLev(one_it_4_ppr.attributeValue("val"));
                                break;
                            case "rPr":
                                Iterator<?> one_it_4_ppr_rpr = one_it_4_ppr.elementIterator();
                                while (one_it_4_ppr_rpr.hasNext()) {
                                    Element one_it_4_ppr_rpr_ = (Element) one_it_4_ppr_rpr.next();
                                    switch (one_it_4_ppr_rpr_.getName()) {
                                        case "rFonts":
                                            ppr.setFontFamily(one_it_4_ppr_rpr_.attributeValue("ascii"));
                                            break;
                                        case "sz":
                                            ppr.setFontSize(Double.parseDouble(one_it_4_ppr_rpr_.attributeValue("val")) / 2 + "");
                                            break;
                                        case "color":
                                            ppr.setFontColor(one_it_4_ppr_rpr_.attributeValue("val"));
                                            break;
                                        case "highlight":
                                            if ("darkYellow".equals(one_it_4_ppr_rpr_.attributeValue("val"))) {
                                                ppr.setHighlight("Olive");
                                            } else {
                                                ppr.setHighlight(one_it_4_ppr_rpr_.attributeValue("val"));
                                            }
                                            break;
                                        case "u":
                                            ppr.setXiaHua(true);
                                            break;
                                        case "b":
                                            ppr.setBold(true);
                                            break;
                                        case "strike":
                                            ppr.setShanChu(true);
                                            break;
                                        case "i":
                                            ppr.setXieTi(true);
                                            break;
                                    }
                                }
                                break;
                            case "ind":
                                Ind ind = new Ind();
                                String marginLeft = one_it_4_ppr.attributeValue("left");
                                if (marginLeft != null && !marginLeft.trim().isEmpty()) {
                                    ind.setLeft(Float.parseFloat(marginLeft));
                                }
                                String firstLine = one_it_4_ppr.attributeValue("firstLine");
                                if (firstLine != null && !firstLine.isEmpty()) {
                                    ind.setFirstLine(Float.parseFloat(firstLine));
                                }
                                String hanging = one_it_4_ppr.attributeValue("hanging");
                                if (hanging != null && !hanging.isEmpty()) {
                                    ind.setHanging(Float.parseFloat(hanging));
                                }
                                ppr.setInd(ind);
                                break;
                            case "jc":
                                String align = one_it_4_ppr.attributeValue("val");
                                if (align != null) {
                                    ppr.setAlign(align);
                                }
                                break;
                            case "numPr":
                                String numId = one_it_4_ppr.element("numId").attributeValue("val");
                                if (wordNumberFormat.get(numId) != null && numId != null && !numId.isEmpty()) {
                                    ppr.setNumberFormat(wordNumberFormat.get(numId) + "-" + numId);
                                    numberFormat_num.merge(ppr.getNumberFormat(), 1, Integer::sum);
                                    ppr.setNumberFormatText(WordNumberConfig.getNumberOfHtml(ppr.getNumberFormat(),
                                            numberFormat_num.get(ppr.getNumberFormat())));
                                }
                                break;
                            case "shd":
                                ppr.setShd(one_it_4_ppr.attributeValue("fill"));
                                break;
                        }
                    }
                    break;
                case "r":
                    RPR rpr = new RPR();
                    Iterator<?> it_4_rpr = one.elementIterator();
                    while (it_4_rpr.hasNext()) {
                        Element one_it_4_rpr = (Element) it_4_rpr.next();
                        switch (one_it_4_rpr.getName()) {
                            case "rPr":
                                Iterator<?> it_4_rpr_sty = one_it_4_rpr.elementIterator();
                                while (it_4_rpr_sty.hasNext()) {
                                    Element one_it_4_rpr_sty = (Element) it_4_rpr_sty.next();
                                    switch (one_it_4_rpr_sty.getName()) {
                                        case "rFonts":
                                            rpr.setFontFamily(one_it_4_rpr_sty.attributeValue("ascii"));
                                            break;
                                        case "color":
                                            rpr.setFontColor(one_it_4_rpr_sty.attributeValue("val"));
                                            break;
                                        case "sz":
                                            rpr.setFontSize(Double.parseDouble(one_it_4_rpr_sty.attributeValue("val")) / 2 + "");
                                            break;
                                        case "highlight":
                                            if ("darkYellow".equals(one_it_4_rpr_sty.attributeValue("val"))) {
                                                rpr.setHighlight("Olive");
                                            } else {
                                                rpr.setHighlight(one_it_4_rpr_sty.attributeValue("val"));
                                            }
                                            break;
                                        case "u":
                                            rpr.setXiaHua(true);
                                            break;
                                        case "b":
                                            rpr.setBold(true);
                                            break;
                                        case "strike":
                                            rpr.setShanChu(true);
                                            break;
                                        case "i":
                                            rpr.setXieTi(true);
                                            break;
                                    }
                                }
                                break;
                            case "t":
                                rpr.setContent(htmlEncode(one_it_4_rpr.getText()));
                                break;
                            case "tab":
                                rpr.setContent("&nbsp;&nbsp;&nbsp;&nbsp;");
                                break;
                            case "drawing":
                                Iterator<?> it_4_rpr_drawing = one_it_4_rpr.elementIterator();
                                if (imagePathsSize <= imageNum) {
                                    break;
                                }
                                rpr.setImgPath(imagePaths.get(imageNum));
                                imageNum++;
                                while (it_4_rpr_drawing.hasNext()) {
                                    Element one_it_4_rpr_drawing = (Element) it_4_rpr_drawing.next();
                                    if (one_it_4_rpr_drawing.getName().equals("inline")) {
                                        Iterator<?> it_4_rpr_drawing_inline = one_it_4_rpr_drawing.elementIterator();
                                        while (it_4_rpr_drawing_inline.hasNext()) {
                                            Element one_it_4_rpr_drawing_inline = (Element) it_4_rpr_drawing_inline.next();
                                            if (one_it_4_rpr_drawing_inline.getName().equals("extent")) {
                                                rpr.setType(1);
                                                String width = one_it_4_rpr_drawing_inline.attributeValue("cx");
                                                if (width != null) {
                                                    rpr.setImgWidth(Float.parseFloat(width) / 9525 + "");
                                                }
                                                String height = one_it_4_rpr_drawing_inline.attributeValue("cy");
                                                if (height != null) {
                                                    rpr.setImgHeight(Float.parseFloat(height) / 9525 + "");
                                                }
                                            }
                                        }
                                    }
                                }
                        }
                    }
                    ppr.addRprs(rpr);
                    break;
            }
        }
        return ppr;
    }

    /**
     * 将html中的<>转义，以免文本中出现后北当做html标签
     *
     * @param source 需要转义的字符串
     * @return 转以后的字符串
     */
    private static String htmlEncode(String source) {
        if (source == null) {
            return "";
        }
        return source.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }

}
